package com.bjpowernode.crm.workbench.clue.controller;

import com.bjpowernode.crm.commons.Constant;
import com.bjpowernode.crm.commons.PagationVo;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.Result;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.clue.service.ClueService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.Clue;
import com.bjpowernode.crm.workbench.domain.ClueActivityRelation;
import com.bjpowernode.crm.workbench.domain.ClueRemark;
import com.bjpowernode.crm.workbench.service.UserService;
import javafx.scene.control.Alert;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: ClueController<br/>
 * Package: com.bjpowernode.crm.workbench.clue.controller<br/>
 * Description: <br/>
 * Date: 2022/7/10 3:08<br/>
 * Auther:对的我写的，错的我同事写的<br/>
 */
@Controller
public class ClueController {
    @Resource
    private ClueService clueService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserService userService;

    @RequestMapping("/workbench/clue/index.do")
    public String toClueIndexPage(HttpServletRequest request){
        BoundListOperations operations = redisTemplate.boundListOps("source");
        List<DicValue> sourceList  = operations.range(0, -1);
        if(sourceList ==null || sourceList.size()==0)
        {
            sourceList = clueService.selectSourceAndState("source");
            for (DicValue dicValue : sourceList)
            {
                operations.leftPush(dicValue);
            }
            operations.expire(DateUtils.getRemainSecondsOneDay(new Date()), TimeUnit.SECONDS);
        }
        request.setAttribute("sourceList",sourceList);
        BoundListOperations operations2 = redisTemplate.boundListOps("clueState");
        List<DicValue> clueStateList = operations2.range(0, -1);
        if(clueStateList == null || clueStateList.size() ==0)
        {
             clueStateList= clueService.selectSourceAndState("clueState");
            for(DicValue dicValue : clueStateList)
            {
                operations2.leftPush(dicValue);
            }
            operations2.expire(DateUtils.getRemainSecondsOneDay(new Date()),TimeUnit.SECONDS);
        }
        request.setAttribute("clueStateList",clueStateList);
        return "workbench/clue/index";
    }

    @RequestMapping("/workbench/clue/selectClues")
    @ResponseBody
    public Object selectClues(@RequestParam(value = "owner",required = false)String owner,
                              @RequestParam(value = "company",required = false)String company,
                              @RequestParam(value = "phone",required = false)String phone,
                              @RequestParam(value = "source",required = false)String source,
                              @RequestParam(value = "fullName",required = false)String fullName,
                              @RequestParam(value = "mphone",required = false)String mphone,
                              @RequestParam(value = "state",required = false)String state,
                              @RequestParam(value = "pageSize",required = true)Integer pageSize,
                              @RequestParam(value = "pageNum",required = true)Integer pageNum)

    {
        Map paramMap = new HashMap();
        paramMap.put("owner",owner);
        paramMap.put("company",company);
        paramMap.put("phone",phone);
        paramMap.put("source",source);
        paramMap.put("fullName",fullName);
        paramMap.put("mphone",mphone);
        paramMap.put("state",state);
        paramMap.put("pageSize",pageSize);
        paramMap.put("pageNum",(pageNum-1)*pageSize);
        PagationVo<Clue> cluePagationVo = clueService.selectClues(paramMap);
        return cluePagationVo;
    }
    @RequestMapping("/workbench/clue/createClues")
    @ResponseBody
    public Object selectClue()
    {
        List<User> userList= userService.openaddActivity();
        List<DicValue> clueStateList= clueService.selectSourceAndState("clueState");
        List<DicValue> sourceList= clueService.selectSourceAndState("source");
        List<DicValue> appellationList= clueService.selectSourceAndState("appellation");
        Map map = new HashMap();
        map.put("userList",userList);
        map.put("clueStateList",clueStateList);
        map.put("sourceList",sourceList);
        map.put("appellationList",appellationList);
        return map;
    }
    @RequestMapping("/workbench/clue/editClues")
    @ResponseBody
    public Object editClue(String id)
    {
        List<User> userList= userService.openaddActivity();
        List<DicValue> clueStateList= clueService.selectSourceAndState("clueState");
        List<DicValue> sourceList= clueService.selectSourceAndState("source");
        List<DicValue> appellationList= clueService.selectSourceAndState("appellation");
        Clue clue = clueService.selectclueById(id);
        Map map = new HashMap();
        map.put("userList",userList);
        map.put("clueStateList",clueStateList);
        map.put("sourceList",sourceList);
        map.put("appellationList",appellationList);
        map.put("clue",clue);
        return map;
    }
    @RequestMapping("/saveCreateClueBtn")
    @ResponseBody
    public Object saveCreateClueBtn(Clue clue,HttpServletRequest request)
    {
        User user = (User) request.getSession().getAttribute(Constant.SESSION);
        try {
            clue.setId(UUIDUtils.getUUID());
            clue.setCreateBy(user.getId());
            clue.setCreateTime(DateUtils.formatDateTime(new Date()));
            Integer count = clueService.insertClue(clue);
            if(count != 1)
            {
                return Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("添加失败！");
        }
        return Result.success();
    }

    @RequestMapping("/edit_update_btn")
    @ResponseBody
    public Object edit_update_btn(Clue clue,HttpServletRequest request)
    {
        User user = (User) request.getSession().getAttribute(Constant.SESSION);
        try {
            clue.setEditBy(user.getId());
            clue.setEditTime(DateUtils.formatDateTime(new Date()));
            Integer count = clueService.updateClue(clue);
            if(count != 1)
            {
                return Result.fail("更新失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("更新失败！");
        }
        return Result.success();
    }
    @RequestMapping("/deleteBtn")
    @ResponseBody
    public Object deleteBtn(@RequestParam(value = "id") String[] ids)
    {
        try {
            Integer count = clueService.delectClueByIds(ids);
            if(count != 1)
            {
                return Result.fail("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("删除失败！");
        }
        return Result.success();
    }
    @RequestMapping("/workbench/clue/detailClue.do")
    public String detailClue(String id,HttpServletRequest request)
    {
        Clue clue = clueService.selectclueById(id);
        request.setAttribute("clue",clue);
        return "workbench/clue/detail";
    }
    @RequestMapping("/getRemarksById")
    @ResponseBody
    public Object getRemarksById(String id)
    {
        List<ClueRemark> remarks = clueService.selectRemarkById(id);
        return remarks;
    }
    @RequestMapping("/save_remark_btn.do")
    @ResponseBody
    public Object saveRemarkBtn(ClueRemark remark,HttpServletRequest request)
    {
        User u = (User) request.getSession().getAttribute(Constant.SESSION);
        try {
            remark.setId(UUIDUtils.getUUID());
            remark.setCreateBy(u.getId());
            remark.setCreateTime(DateUtils.formatDateTime(new Date()));
            Integer count = clueService.insertClueRemark(remark);
            if(count != 1)
            {
                return Result.fail("留言失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("留言失败！");
        }
        return Result.success();
    }

    @ResponseBody
    @RequestMapping("/getactivityByClue.do")
    public Object getactivityByClue(String clueId)
    {
       List<Activity> activityList = clueService.selectActivitiesByClueId(clueId);
       return activityList;
    }
    @ResponseBody
    @RequestMapping("/deleteActivityBtn.do")
    public Object deleteActivityBtn(@RequestParam(value = "activityId")String activityId,
                                    @RequestParam(value = "clueId")String clueId)
    {
        Map map = new HashMap();
        map.put("clueId",clueId);
        map.put("activityId",activityId);
        try {
            Integer count = clueService.deleteActivityByactivityIdAndclueId(map);
            if(count <=0)
            {
                return Result.fail("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("删除失败！");
        }
        return Result.success();
    }
    @RequestMapping("/bundActivityBtn.do")
    @ResponseBody
    public Object bundActivityBtn(String clueId,String name)
    {
        Map map =new HashMap();
        map.put("clueId",clueId);
        map.put("name",name);
        List<Activity> activityList = clueService.selectActivityByactivityIdAndclueId(map);
        return activityList;
    }

    @RequestMapping("/saveBundActivityBtn.do")
    @ResponseBody
    public Object saveBundActivity(@RequestParam(value = "activityId")String[] activityId,
                                   @RequestParam(value = "clueId")String clueId)
    {
        List list =new ArrayList();
        ClueActivityRelation relation = null;
        for (int i=0;i<activityId.length;i++)
        {
            relation=new ClueActivityRelation();
            relation.setId(UUIDUtils.getUUID());
            relation.setActivityId(activityId[i]);
            relation.setClueId(clueId);
            list.add(relation);
        }
        try {
            Integer count = clueService.insertClueActivityRelation(list);
            if(count == 0)
            {
                return Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("添加失败！");
        }
        return Result.success();
    }

}
